//
//  LLT_PlayerInfoViewController_LXM.m
//  CoderLLT
//
//  Created by 刘晓萌 on 15/12/15.
//  Copyright © 2015年 刘晓萌. All rights reserved.
//

#import "LLT_PlayerInfoViewController_LXM.h"
#import "LLT_PlayerCells_LXM.h"
#import "LLT_DataHelper_LXM.h"
#import "UIImageView+WebCache.h"
#import "LLT_HeroInfoViewController_LXM.h"


#import "LLT_MenuTableViewController_LXH.h"
#import "UIViewController+REFrostedViewController.h"
#import "REFrostedViewController.h"

@interface LLT_PlayerInfoViewController_LXM ()<UITableViewDelegate,UITableViewDataSource,UIWebViewDelegate,UIScrollViewDelegate>

@property (weak, nonatomic) IBOutlet UIImageView *playerHeader;
@property (weak, nonatomic) IBOutlet UILabel *playerName;
@property (weak, nonatomic) IBOutlet UIView *playerInfoShowView;
@property (weak, nonatomic) IBOutlet UIWebView *playerInfoShowWebView;
@property (weak, nonatomic) IBOutlet UITableView *playerTableView;

@property (weak, nonatomic) IBOutlet UIButton *leftButton;
@property (weak, nonatomic) IBOutlet UIButton *rightButtonShare;
@property (weak, nonatomic) IBOutlet UIButton *rightButtonSearch;

@property (strong, nonatomic) NSMutableArray *playerRecordArr;
@property (strong, nonatomic) NSMutableArray *playerLastUseArr;
@property (strong, nonatomic) NSMutableArray *playerHeroRecordArr;
@property (assign, nonatomic) NSInteger indexScrollFlag;

@property (weak, nonatomic) IBOutlet UIImageView *flagImageView;
@property (weak, nonatomic) IBOutlet UILabel *FirstTitleLable;
@property (weak, nonatomic) IBOutlet UILabel *secondTitleLable;
@property (weak, nonatomic) IBOutlet UIScrollView *playerScroll;

@end

@implementation LLT_PlayerInfoViewController_LXM



- (void)viewDidLoad {
    [super viewDidLoad];
    [self makeDelegate];
    [self setView];
    [self makeData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark -- 数据解析处理/调用dataHleper获取数据
-(void)makeData{
    LLT_DataHelper_LXM *dataHelper =  [[LLT_DataHelper_LXM alloc]init];
    __weak typeof(self) weakSelf = self;
    __weak UITableView *weakTable = self.playerTableView;
    //获取用户数据block
    dataHelper.setPlayerInfo = ^(LLT_PlayerModel *player){
        weakSelf.player = player;
        [weakSelf setplayerInfoShowWebView];
    };
    //获取用户段位block
    dataHelper.setPlayerRankClass = ^(LLT_PlayerModel *player){
        weakSelf.player.rankClass = player.rankClass;
        NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:0];
        [weakTable reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];

    };
    self.playerRecordArr = [NSMutableArray array];
    //获取用户战绩统计blok
    dataHelper.setPlayerRecord = ^(NSMutableArray *recordArr){
        weakSelf.playerRecordArr = recordArr;
        NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:1];
        [weakTable reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
    };
    //获取用户常用英雄blok
    dataHelper.setPlayerLastUsed = ^(NSMutableArray *playerLastUseArr){
        weakSelf.playerLastUseArr = playerLastUseArr;
        NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:2];
        [weakTable reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];    };
    //获取用户英雄使用统计block
    dataHelper.setPlayerHeroRecord =^(NSMutableArray *playerHeroRecordArr){
        weakSelf.playerHeroRecordArr = playerHeroRecordArr;
        NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:3];
        [weakTable reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
    };
    //--- 开始调用方法 ---
    //获取用户信息
    [dataHelper getPlayerDetailByPlayerName:self.player.playerName andServerName:self.player.serverName];
    //获取用户段位
    [dataHelper getPlayerRankClassByPlayerName:self.player.playerName andServerName:self.player.serverName];
    //获取用户战绩
    [dataHelper getPlayerRecordByPlayerName:self.player.playerName andServerName:self.player.serverName];
    //获取用户的常用英雄
    [dataHelper getPlayerLastUsedByPlayerName:self.player.playerName andServerName:self.player.serverName];
    //获取用户英雄使用统计
    [dataHelper getPlayerHeroRecordByPlayerName:self.player.playerName andServerName:self.player.serverName];
}
#pragma mark -- 设置webView界面
-(void)setplayerInfoShowWebView{
    NSString *url = [NSString stringWithFormat:@"http://api.lolmax.com/api/player/power_value_web/?area_id=%@&uid=%@&phone_num=00000000000",self.player.serverID,self.player.playerName];
    url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [self.playerInfoShowWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
    
}
#pragma mark -- 重写Player
-(void)setPlayer:(LLT_PlayerModel *)player{
    if (_player != player) {
        _player = nil;
        _player = player;
    }
    [self.playerHeader sd_setImageWithURL:[NSURL URLWithString:_player.playerIcon]];
    self.playerName.text = _player.playerName;
}
#pragma mark -- 接收代理/注册cell
-(void)makeDelegate{
    self.playerScroll.delegate = self;
    self.playerTableView.delegate = self;
    self.playerTableView.dataSource = self;
    self.playerInfoShowWebView.delegate = self;
    //注册xib的cell
    [self.playerTableView registerNib:[UINib nibWithNibName:@"LLT_PlayerAreaViewCell_LXM" bundle:nil] forCellReuseIdentifier:@"pareaCell"];
    [self.playerTableView registerNib:[UINib nibWithNibName:@"LLT_PlayerMatchViewCell_LXM" bundle:nil] forCellReuseIdentifier:@"matchCell"];
    [self.playerTableView registerNib:[UINib nibWithNibName:@"LLT_PlayerMatchTitleViewCell_LXM" bundle:nil] forCellReuseIdentifier:@"matchTitleCell"];
    [self.playerTableView registerNib:[UINib nibWithNibName:@"LLT_PlayerAlwaysUseHeroViewCell_LXM" bundle:nil] forCellReuseIdentifier:@"alwaysUseCell"];
    [self.playerTableView registerNib:[UINib nibWithNibName:@"LLT_PlayerHeroViewCell_LXM" bundle:nil] forCellReuseIdentifier:@"heroViewCell"];
    [self.playerTableView registerNib:[UINib nibWithNibName:@"LLT_PlayerHeroTitleViewCell_LXM" bundle:nil] forCellReuseIdentifier:@"heroTitleViewCell"];
    
}
#pragma mark -- 设置界面
-(void)setView{
    if (!self.player) {
        self.player = [LLT_PlayerModel new];
#pragma mark -- 后期处理
        
    }
    if (self.showFlag) {
        [self.leftButton setImage:[UIImage imageNamed:@"iconfont-yinlefanhui"] forState:UIControlStateNormal];
        self.rightButtonShare.hidden = YES;
        [self.rightButtonSearch setImage:[UIImage imageNamed:@"share"] forState:UIControlStateNormal];
    }
    UITapGestureRecognizer *leftTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(didClickLeftTitleLabelAction)];
    UITapGestureRecognizer *rightTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(didClickRightTitleLabelAction)];
    [self.FirstTitleLable addGestureRecognizer:leftTap];
    [self.secondTitleLable addGestureRecognizer:rightTap];
    
    self.FirstTitleLable.userInteractionEnabled = YES;
    self.secondTitleLable.userInteractionEnabled = YES;
    SetTabBarTintColor;
    self.playerHeader.layer.masksToBounds = YES;
    self.playerHeader.layer.cornerRadius = self.playerHeader.frame.size.width/2;
    self.secondTitleLable.textColor = [UIColor grayColor];
}

#pragma mark --- scrollView代理方法和 滑动点击
/*
 * scrollView滑动调用方法
 */
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    self.indexScrollFlag = self.playerScroll.contentOffset.x/self.view.frame.size.width;
    if (self.indexScrollFlag) {
        self.secondTitleLable.textColor = [UIColor whiteColor];
        self.FirstTitleLable.textColor = [UIColor grayColor];
        CGFloat x = self.secondTitleLable.center.x - (self.flagImageView.frame.size.width/2);
        [UIView animateWithDuration:0.3 animations:^{
            self.flagImageView.frame = CGRectMake(x, self.flagImageView.frame.origin.y,self.flagImageView.frame.size.width,self.flagImageView.frame.size.height);
        }];
        
    }else{
        self.secondTitleLable.textColor = [UIColor grayColor];
        self.FirstTitleLable.textColor = [UIColor whiteColor];
        CGFloat x = self.FirstTitleLable.center.x - (self.flagImageView.frame.size.width/2);
        [UIView animateWithDuration:0.3 animations:^{
            self.flagImageView.frame = CGRectMake(x, self.flagImageView.frame.origin.y,self.flagImageView.frame.size.width,self.flagImageView.frame.size.height);
        }];
    }
    
}

/**
 * 滑动点击方法
 */
-(void)didClickLeftTitleLabelAction{
        self.playerScroll.contentOffset = CGPointMake(0, self.playerScroll.contentOffset.y);
}
-(void)didClickRightTitleLabelAction{
        self.playerScroll.contentOffset = CGPointMake(self.view.frame.size.width, self.playerScroll.contentOffset.y);
}

#pragma mark -- tableView的代理方法
/**
 * 每个section有多少行
 */
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0 || section == 2) {
        return 1;
    }
    if (section == 1){
        return (self.playerRecordArr.count+1);
    }else {
        return (self.playerHeroRecordArr.count + 1);
    }
}

/**
 * 有多少分组
 */
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}

/**
 * 每一行高度
 */
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSInteger section = indexPath.section;
    NSInteger row = indexPath.row;
    if (section == 0) {
        return 60;
    }
    if (section == 1 || section == 3) {
        if (row == 0) {
            return 20;
        }
        if (section == 3) {
            return 50;
        }
    }
    if (section == 2) {
        return 103;
    }
    return 40;
}

/**
 * 设置uitableview的cell
 */
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSInteger section = indexPath.section;
    NSInteger row = indexPath.row;
    if(section == 0){
        LLT_PlayerAreaViewCell_LXM *cell = [tableView dequeueReusableCellWithIdentifier:@"pareaCell" forIndexPath:indexPath];
        cell.player = self.player;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    else if(section == 1){
        if (row == 0) {
            LLT_PlayerMatchTitleViewCell_LXM *cell = [tableView dequeueReusableCellWithIdentifier:@"matchTitleCell" forIndexPath:indexPath];
            return cell;
        }else{
            NSInteger index = row - 1;
            LLT_PlayerMatchViewCell_LXM *cell = [tableView dequeueReusableCellWithIdentifier:@"matchCell" forIndexPath:indexPath];
            LLT_PlayerRecordModel *playerRM = self.playerRecordArr[index];
            cell.playerRecordModel = playerRM;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }
    }
    else if(section == 2){
        LLT_PlayerAlwaysUseHeroViewCell_LXM *cell = [tableView dequeueReusableCellWithIdentifier:@"alwaysUseCell" forIndexPath:indexPath];
        cell.heroArr = self.playerLastUseArr;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    else {
        if (row == 0) {
            LLT_PlayerHeroTitleViewCell_LXM *cell = [tableView dequeueReusableCellWithIdentifier:@"heroTitleViewCell" forIndexPath:indexPath];
            return cell;
        }else{
            LLT_PlayerHeroViewCell_LXM *cell = [tableView dequeueReusableCellWithIdentifier:@"heroViewCell" forIndexPath:indexPath];
            NSInteger index = row - 1;
            LLT_PlayerHeroRecordModel *heroRM = self.playerHeroRecordArr[index];
            cell.heroRecord = heroRM;
            return cell;
        }
    }
}

/**
 * 设置每个section的head的内容
 */
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if(section == 1){
        return @"个人战绩统计";
    }
    if (section == 2) {
        return @"最近常用英雄";
    }
    if (section == 3) {
        return @"英雄使用统计";
    }
    return @"";
}

/**
 * 设置每个section的head的高度
 */
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 0.001;
    }
    return 10;
}


/**
 *设置分割线顶格
 */
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

/**
 *设置cell点击事件
 */
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSInteger section = indexPath.section;
    NSInteger row = indexPath.row;
    if (section == 3 && row > 0) {
        LLT_PlayerHeroRecordModel *phrm = _playerHeroRecordArr[row - 1];
        NSString *p1 = [NSString stringWithFormat:@"heroInfo.html?id=%@",phrm.championName];
        NSString *p2 = [NSString stringWithFormat:@"heroGear.html?id=%@",phrm.championName];
        NSString *p3 = [NSString stringWithFormat:@"heroEnemy.html?id=%@",phrm.championName];
        NSString *p4 = [NSString stringWithFormat:@"heroPartner.html?id=%@",phrm.championName];
        NSString *pic = HeroPic(phrm.championName);
        LLT_DetailOfArticleModel *urls = [LLT_DetailOfArticleModel new];
        NSDictionary *dic1 = @{@"name":@"英雄详情",@"path":p1};
        NSDictionary *dic2 = @{@"name":@"使用物品",@"path":p2};
        NSDictionary *dic3 = @{@"name":@"克制物品",@"path":p3};
        NSDictionary *dic4 = @{@"name":@"配合英雄",@"path":p4};
        NSArray *pages = @[dic1,dic2,dic3,dic4];
        urls.pic = pic;
        urls.rootUrl = @"http://static.lolmax.com/api/static/lol/pages/hero/";
        urls.title = phrm.championNameCN;
        urls.pages = pages.mutableCopy;
        LLT_HeroInfoViewController_LXM *heroIVC = [MainStoryBoard instantiateViewControllerWithIdentifier:@"heroInfoVC"];
        heroIVC.heroUrls = urls;
        [self presentViewController:heroIVC animated:YES completion:nil];
    }
}

#pragma mark -- 界面按钮点击事件

/**
 * 点击左侧个人设置按钮
 *
 *  @param sender
 */
- (IBAction)didClickLeftSettingButton:(UIButton *)sender {
    if (self.showFlag) {
        DissMissBack;
        //NSLog(@"返回");
    }else{
        //NSLog(@"个人详情");
        [self showMenu];
    }
    
}
-(void)showMenu
{
    [self.frostedViewController presentMenuViewController];
}

/**
 * 设置电源栏反颜色
 *
 * @return
 */
- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
